
use $datapath/clean/main_data.dta, clear 

************************************************
* Create variable labels: 

	label var Qp "phys"
	label var Np "phys holdings"
	label var Qe "ebook"
	label var Ne "ebook holdings"

			
**********************************************************************
** create jump variables


************************************
** ebook jumps 
	
	gsort lno year 
	
** levels: 	
	gen ex = Ne-Ne[_n-1] if lno==lno[_n-1] 
	su ex, de
		local etd = r(p90)

** jump cutoff: 
gsort lno year
	gen ee = (Ne-Ne[_n-1] >`etd') & lno==lno[_n-1] & (Ne+Ne[_n-1]~=.) & (Ne[_n+1]-Ne >=0)
	
******************


******************
** create IVs

	gsort lno year
		by lno: gen ive =sum(ee) 
		label var ive "ebook jump"


**********************************************************************


**********************************************************************
** Reshape data to run one regression for all

	keep Qp Qe Np Ne year lno ive 
	gen num=_n 
	reshape long Q, i(Np Ne year lno num ive) j(type) s 

	gen de = type=="e"
	egen lfno = group(lno de)
	egen yfno = group(year de)

**********************************************************************


**********************************************************************
** Fixed effects:

areg Q i.de#c.Np i.de#c.Ne i.yfno, absorb(lfno )  vce(cluster lno )

	nlcom ((2/3)*_b[0.de#c.Np] - _b[0.de#c.Ne]) / (_b[1.de#c.Ne] - (2/3)*_b[1.de#c.Np])
	testnl _b[0.de#c.Ne] + _b[1.de#c.Ne] = 0

	nlcom ((0.235)*_b[0.de#c.Np] - _b[0.de#c.Ne]) / (_b[1.de#c.Ne] - (0.235)*_b[1.de#c.Np])
	testnl _b[0.de#c.Ne] + _b[1.de#c.Ne] = 0
**********************************************************************



**********************************************************************
** IV:

** First jump year:
	gen xe = year if ive==1   
	egen Ymin2 = min(xe), by(lno)
	
** total number of jumps:  
	egen total_jumps = max(ive), by(lno)
	
** Set up observations we want to include 
	gen ifstatement = (ive <2 & year<=Ymin+1 & year >= Ymin-2) | total_jumps==0

	
	
ivreghdfe Q i.de#c.Np (i.de#c.Ne = i.de#c.ive) if ifstatement==1, absorb(lfno yfno)  cluster(lfno)

	nlcom ((2/3)*_b[0.de#c.Np] - _b[0.de#c.Ne]) / (_b[1.de#c.Ne] - (2/3)*_b[1.de#c.Np])
	testnl _b[0.de#c.Ne] + _b[1.de#c.Ne] = 0

	nlcom ((0.235)*_b[0.de#c.Np] - _b[0.de#c.Ne]) / (_b[1.de#c.Ne] - (0.235)*_b[1.de#c.Np])
	testnl _b[0.de#c.Ne] + _b[1.de#c.Ne] = 0
**********************************************************************

	
	
	
	
	
	